//
//  SettingPayPasswrodVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/29.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "SettingPayPasswrodVC.h"
#import "Tool.h"
#import "NSUDManager.h"
#import "NSString+Expand.h"
#import "NSString+Valid.h"
@interface SettingPayPasswrodVC ()
@property (weak, nonatomic) IBOutlet UIButton *sendCodeButton;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *codeTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UITextField *againPasswordTextField;
/* 定时器 */
@property(strong,nonatomic) NSTimer *timer;
/* 定时多少秒 */
@property(assign,nonatomic) NSInteger count;
@end

@implementation SettingPayPasswrodVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationItem.title = @"设置支付/提现密码";
    self.view.backgroundColor = UIColorFromRGB(0XF2F5FA);
    [self myInfo];
    
}
- (IBAction)sendCodeClick:(UIButton *)sender {
    if (!self.phoneTextField.text || self.phoneTextField.text.length == 0) {
        [CommonUtils showToast:@"请输入手机号"];
        return;
    }
    if (![Tool validMobile:self.phoneTextField.text]) {
        [CommonUtils showToast:@"请输入正确的手机号"];
        return;
    }
    
    
    
    
    WS(weakSelf)
    [self showProgressWithView:self.view animated:YES];
    [SendRequest sendVerificationCodeWithTel:self.phoneTextField.text type:@"modifyPayPwd" result:^(NSDictionary *result, NSError *error) {
        [weakSelf hideProgress:self.view animated:YES];
        result = [CommonUtils decryptDicWithResult:result];
        if([result[@"result"] integerValue] == 1) {
            weakSelf.count = 59;
            [weakSelf.sendCodeButton setTitle:[NSString stringWithFormat:@"%zds", self.count] forState:UIControlStateNormal];
            weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeDown) userInfo: nil repeats:YES];
            weakSelf.sendCodeButton.userInteractionEnabled = NO;
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
        
    }];
    
    
}
- (void)timeDown {
    if (self.count != 1) {
        self.count -= 1;
        [self.sendCodeButton setTitle:[NSString stringWithFormat:@"%zds", self.count] forState:UIControlStateNormal];
    } else {
        self.sendCodeButton.userInteractionEnabled = YES;
        [self.sendCodeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
        [self.timer invalidate];
        self.timer = nil;
    }
}
-(void)myInfo{
    UserModel *user = [NSUDManager currentUser];

    [YJMSendRequest postDataWithParameters:@{@"userId":user.userId} url:@"user/getIncumbentInfo" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            self.phoneTextField.text = result[@"data"][@"userInfo"][@"mobile"];
           
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
    
}
- (IBAction)finishClick:(id)sender {
    if (self.codeTextField.text.length == 0) {
        ALERT_VIEW(@"请输入验证码");
        return;
    }else  if (self.passwordTextField.text.length != 0 || self.againPasswordTextField.text.length != 0) {
        ALERT_VIEW(@"请输入6位数字密码");
        return;
    }else  if ([NSString CheckIntValueInput:self.passwordTextField.text]) {
        ALERT_VIEW(@"请输入6位数字密码");
        return;
    }else  if (![self.passwordTextField.text isEqualToString:self.againPasswordTextField.text]) {
        ALERT_VIEW(@"两次密码不一致");
        return;
    }
    
    [YJMSendRequest postDataWithParameters:@{@"payPwd":self.passwordTextField.text,@"smsCode":self.codeTextField.text} url:@"user/updatePayPwd" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
